package entity

import (
	"fmt"
	"strings"
)

type NodeType string

const (
	NodeTypeStart        NodeType = "start"
	NodeTypeEnd          NodeType = "end"
	NodeTypeLLM          NodeType = "llm"
	NodeTypeChatModel    NodeType = "chatmodel"
	NodeTypeChatTemplate NodeType = "chatTemplate"
	NodeTypeMcpTemplate  NodeType = "chatMcpTemplate"
	NodeTypeMcpTool      NodeType = "mcpTool"
	NodeTypeBranch       NodeType = "branch"
	NodeTypeLoop         NodeType = "loop"
	NodeTypeLoader       NodeType = "loader"
	NodeTypeEvent        NodeType = "event"
)

type IVariable interface {
	Type() string
	Value() interface{}
}

type Variable struct {
	T string
}

func (v Variable) Type() string {
	return v.T
}

func (v Variable) Value() interface{} {
	return nil
}

// 字符串型
type VarString struct {
	Variable
	Default string
}

// 布尔型
type VarBoolean struct {
	Variable
	Default bool
}

// 引用
type VarRef struct {
	Variable
	Content []string
}

// 数组
type VarArray struct {
	Variable
	Items Variable //数组对象的内容定义
}

// 对象
type VarObject struct {
	Variable
	Properties map[string]Variable
}

// 常量
type VarConst struct {
	Variable
	Content interface{}
}

func (v VarConst) Value() interface{} {
	return v.Content
}

// 条件
type ConditionValue struct {
	Left     Variable
	Operator string
	Right    Variable
}

type Condition struct {
	Key   string
	Value ConditionValue
}

func parseConstantVeriable(raw map[string]interface{}) (IVariable, error) {
	var ok bool
	veriable := VarConst{}
	veriable.T = "constant"
	veriable.Content, ok = raw["content"].(interface{})
	if !ok {
		return veriable, fmt.Errorf("Constant 类型解析错误")
	}

	return veriable, nil
}

func parseVeriable(raw map[string]interface{}) (IVariable, error) {
	var veriable IVariable
	var err error

	Type, ok := raw["type"].(string)
	if !ok {
		return veriable, fmt.Errorf("变量解析错误")
	}

	switch Type {
	//case "string":
	//	veriable, err = parseStringVeriable(raw)
	//case "number":
	//	veriable = entity.Variable{T: "number"}
	//case "boolean":
	//	veriable, err = parseBoolVeriable(raw)
	//case "object":
	//	veriable, err = parseObjectVeriable(raw)
	//case "array":
	//	veriable, err = parseArrayVeriable(raw)
	case "constant":
		veriable, err = parseConstantVeriable(raw)
		//case "ref":
		//	veriable, err = parseRefVeriable(raw)
	}

	if err != nil {
		return veriable, err
	}

	return veriable, nil
}

func getValue(data map[string]interface{}, keys []string) IVariable {
	current := interface{}(data)
	lastIndex := len(keys) - 1

	for i, key := range keys {
		// 尝试断言为 map[string]interface{}
		m, ok := current.(map[string]interface{})
		if !ok {
			return Variable{}
		}

		current, ok = m[key]
		if !ok || (i == lastIndex) {
			m, ok = current.(map[string]interface{})
			if !ok {
				return Variable{}
			}
			variable, err := parseVeriable(m)
			if err != nil {
				return Variable{}
			}
			return variable
		}
	}

	return nil
}

type Node struct {
	Id          string
	Type        NodeType
	Title       string
	Description string
	Data        map[string]interface{}
}

func (n Node) GetValue(key string) IVariable {
	key = strings.ToLower(key)
	parts := strings.Split(key, ".")

	v := getValue(n.Data, parts)
	return v
}

type Edge struct {
	Id           string
	Source       string
	Target       string
	SourceHandle string
	TargetHandle string
}

type Flow struct {
	Id    uint
	Name  string
	Nodes []Node
	Edges []Edge
}
